<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 短信接口
 * @author calvin<hongbinfu@163.com>
 */

class Sms
{
    /*public $url = "http://222.73.117.158/msg/HttpBatchSendSM";

    private $debug = TRUE;
    //private $curl;
    public $timeout=10;
    public $account = "jiekou-cs-01";
    public $password = "Tch147259";*/
    public $error_code = 0;
    public $setting;
    public $driver;
    public function __construct()
    {
        $this->load->library('curl');
        $this->config->load('sms');
        $this->setting = $this->config->item("sms_api");
        $this->_load_driver();
    }

    protected function _load_driver()
    {
        $common_driver_file = APPPATH."libraries".DIRECTORY_SEPARATOR."Sms".DIRECTORY_SEPARATOR."Sms_driver.php";
        if(file_exists($common_driver_file))
        {
            require_once $common_driver_file;
        }
        else
        {
            throw new UnexpectedValueException("Sms: Sms driver was not found. Aborting.");
        }
        $interface_file = APPPATH."libraries".DIRECTORY_SEPARATOR."Sms".DIRECTORY_SEPARATOR."Sms_driver_interface.php";
        if(file_exists($interface_file))
        {
            require_once $interface_file;
        }
        else
        {
            throw new UnexpectedValueException("Sms: Sms interface was not found. Aborting.");
        }
        $prefix = "Sms_driver_";
        $driver_file = APPPATH."libraries".DIRECTORY_SEPARATOR."Sms".DIRECTORY_SEPARATOR."Driver".DIRECTORY_SEPARATOR.$prefix.$this->setting['driver'].".php";
        if(file_exists($driver_file))
        {
            require_once $driver_file;
            $driver_name = $prefix.$this->setting['driver'];
            $this->driver = new $driver_name();
            $this->driver->load_config($this->setting);
        }
        else
        {
            throw new UnexpectedValueException("Sms: Configured driver '".$this->setting['driver']."' was not found. Aborting.");
        }
    }

    function __get($key)
    {
        $CI = & get_instance();
        return $CI->$key;
    }

    public function get_error()
    {
        return array("code"=>$this->error_code);
    }
    /**
     * 发送短信
     * @param $data
     */
    public function send($mobile,$msg)
    {
        $path = APPPATH.'logs/sms.php';
        if (ENVIRONMENT=="development")
        {
            /*file_put_contents(APPPATH.'logs/sms.txt', json_encode(array('mobile' => $mobile, 'msg' => $msg,'time'=>time())));*/
            /*$this->session->merge_userdata("_SMS_TEST_LIST_",array(array('mobile' => $mobile, 'msg' => $msg,'time'=>time())));*/

            if(file_exists($path))
            {
                $f_data = include_once $path;
                //var_dump($data);exit;
                $data = array();
                foreach($f_data as $v)
                {
                    if(time()-$v['time']<7200)
                    {
                        $data[]=$v;
                    }
                }
            }
            else
            {
                $data = array();
            }
            $data[]=array(
                'mobile' => $mobile, 'msg' => $msg,'time'=>time()
            );
            $content = "<?php return ".var_export($data,TRUE).";";
            file_put_contents($path,$content);
            if($this->setting['driver']=="test")
            {
                return TRUE;
            }
        }
        elseif(file_exists($path))
        {
            @unlink(APPPATH.'logs/sms.txt');
        }
        return $this->driver->send($mobile,$msg);
    }

}

?>